
// Represents the data needed for an entity on the client side.
// @param objId The ObjectNode ID.
// @param id The entity ID.
function Entity(objId, id, scope) {
  this.objId = objId;
  this.id = id;
  this.scope = scope;
}

Entity.prototype.getId = function() {
	if (this.id && (this.id != ""))
		return this.objId + "#" + this.id;
	return this.objId;
}

Entity.prototype.load = function(onComplete) {
  if (!this.editors) {
  	this.editors = new Object();
  	var url = BasePath.get()+EDIT_BASE_PATH+"/EntityInfo.view?objId="+this.objId;
  	if (this.id && (this.id != ""))
  	  url += "&id=" + this.id;
  	if (this.scope)
  	  url += "&scope=" + this.scope;
    var res = new LivCosResource(url, null, StatusDisplay.getInstance());
    res._entity = this;
    res._callback = onComplete;
    res.onSuccess = function() {
      var nodes = this.dom.documentElement.childNodes;
      for (var i = 0; i < nodes.length; ++i) {
        var n = nodes[i];
        var tagName = (n.localName)?n.localName:n.nodeName;
        if ("editor" == tagName) {
        	var id = n.getAttribute("id");
        	if (!id)
        	  id = "--default--";
          this._entity.editors[id] = n;
        }
        if ("menu" == tagName)
          this._entity.menu = n;
      }
      this._callback.call(this._entity);
      this.statusDisplay.show("...entity info loaded", null, true);
    };
    res.errorMessage = "Could not load entity info!";
    res.load();
  }
  else {
    onComplete.call(this);
  }
}

Entity.prototype.fillEditor = function(headerElement, contentElement, editorId) {
  contentElement.innerHTML = "";
  var doc = contentElement.ownerDocument;
  if (!editorId)
    editorId = "--default--";
  var editor = this.editors[editorId];
  if (editor) {
  	var label = editor.getAttribute("label");
  	if (!label)
  		label = "Edit";
 		headerElement.innerHTML = "<span>" + label + "</span> " + headerElement.innerHTML;
 		contentElement.innerHTML = Dom(editor).serializeContent();
  }
  else {
  	contentElement.innerHTML = "<p style='color: red;'>Editor '" + editorId +"' is not available!</p>";
  }
}


